package com.example.rxjavademo;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.example.rxjavademo.base.BaseObserver;
import com.example.rxjavademo.http.RetrofitHelper;
import com.example.rxjavademo.http.SeverAPIService;
import com.example.rxjavademo.model.mtvdetail.Movie;
import com.example.rxjavademo.model.Mvtv;
import com.example.rxjavademo.model.Tag;
import com.example.rxjavademo.model.persondetail.Person;
import com.example.rxjavademo.adapter.MyDataViewAdapter;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener;

import java.util.Arrays;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.schedulers.Schedulers;

@SuppressLint("NonConstantResourceId")
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private MyDataViewAdapter myDataViewAdapter;
    private int page_limit = 10;
    private int page_start = 0;
    private String type = "";
    @BindView(R.id.tv_data)
    TextView tv_data;
    @BindView(R.id.bt_getTags)
    Button bt_getTags;
    @BindView(R.id.bt_getMvList)
    Button bt_getMvList;
    @BindView(R.id.bt_getTvList)
    Button bt_getTvList;
    @BindView(R.id.bt_getMvtvData)
    Button bt_getMvtvData;
    @BindView(R.id.bt_getPersonData)
    Button bt_getPersonData;
    @BindView(R.id.smartrefresh)
    SmartRefreshLayout smartrefresh;
    @BindView(R.id.recyclerview)
    RecyclerView recyclerView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        init();

    }

    private void init(){
        myDataViewAdapter = new MyDataViewAdapter();
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        recyclerView.setAdapter(myDataViewAdapter);
        recyclerView.setNestedScrollingEnabled(false);

        smartrefresh.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                page_start = page_start + page_limit;
                getMtvList(page_limit,page_start,type);
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                page_start = 0;
                getMtvList(page_limit,page_start,type);
            }
        });
    }

    private void getTags(){
        RetrofitHelper.tokenCreate(SeverAPIService.class)
        .getTags("movie", "index")
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(new BaseObserver<Tag>() {
            @Override
            public void onNext(Tag response) {
                super.onNext(response);
                Log.d("onNext", Arrays.toString(response.getTags().toArray()));
                tv_data.setText(Arrays.toString(response.getTags().toArray()));
            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);
            }
        });
    }

    private void getMtvList(int page_limit, int page_start, String type){
        RetrofitHelper.tokenCreate(SeverAPIService.class)
                .getMvtvList(type, "热门", page_limit, page_start)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<Mvtv>() {
                    @Override
                    public void onNext(Mvtv response) {
                        super.onNext(response);
                        if(page_start == 0){
                            myDataViewAdapter.setNewData(response.getSubjects());
                        } else {
                            myDataViewAdapter.addData(response.getSubjects());
                        }
                        smartrefresh.finishLoadMore();
                        smartrefresh.finishRefresh();
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        smartrefresh.finishLoadMore();
                        smartrefresh.finishRefresh();
                    }
                });
    }

    private void getMtvData(){
        RetrofitHelper.tokenCreate(SeverAPIService.class, AppConstants.API_BASEURL)
                .getMvtvData(AppConstants.API_REFERER,"10604086")
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<Movie>() {
                    @Override
                    public void onNext(Movie response) {
                        super.onNext(response);
                        Log.d("onNext", response.toString());
                        tv_data.setText(response.toString());
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                    }
                });
    }

    private void getPersonData(){
        RetrofitHelper.tokenCreate(SeverAPIService.class, AppConstants.API_BASEURL)
                .getPersonData(AppConstants.API_REFERER,"1275211")
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<Person>() {
                    @Override
                    public void onNext(Person response) {
                        super.onNext(response);
                        Log.d("onNext", response.toString());
                        tv_data.setText(response.toString());
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                    }
                });
    }

    @SuppressLint("SetTextI18n")
    @OnClick({R.id.bt_getTags,R.id.bt_getMvList,R.id.bt_getTvList,R.id.bt_getMvtvData,R.id.bt_getPersonData})
    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.bt_getTags :
                getTags();
                break;
            case R.id.bt_getMvList :
                page_start = 0;
                type = "movie";
                getMtvList(page_limit,page_start,type);
                break;
            case R.id.bt_getTvList :
                page_start = 0;
                type = "tv";
                getMtvList(page_limit,page_start,type);
                break;
            case R.id.bt_getMvtvData :
                getMtvData();
                break;
            case R.id.bt_getPersonData :
                getPersonData();
                break;
            default:
                break;
        }
    }

}